using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Data
{
	public static class TypedTableBaseExtensions
	{

		public static EnumerableRowCollection<TRow> AsEnumerable<TRow> (this TypedTableBase<TRow> source) where TRow : DataRow
		{
			return new EnumerableRowCollection<TRow> (source);
		}

		public static OrderedEnumerableRowCollection<TRow> OrderBy<TRow, TKey> (this TypedTableBase<TRow> source, Func<TRow, TKey> keySelector) where TRow : DataRow
		{
			return OrderBy<TRow, TKey> (source, keySelector, Comparer<TKey>.Default);
		}

		public static OrderedEnumerableRowCollection<TRow> OrderBy<TRow, TKey> (this TypedTableBase<TRow> source, Func<TRow, TKey> keySelector, IComparer<TKey> comparer) where TRow : DataRow
		{
			return OrderedEnumerableRowCollection<TRow>.Create<TRow, TKey> (source, keySelector, comparer, false);
		}

		public static OrderedEnumerableRowCollection<TRow> OrderByDescending<TRow, TKey> (this TypedTableBase<TRow> source, Func<TRow, TKey> keySelector) where TRow : DataRow
		{
			return OrderByDescending<TRow, TKey> (source, keySelector, Comparer<TKey>.Default);
		}

		public static OrderedEnumerableRowCollection<TRow> OrderByDescending<TRow, TKey> (this TypedTableBase<TRow> source, Func<TRow, TKey> keySelector, IComparer<TKey> comparer) where TRow : DataRow
		{
			return OrderedEnumerableRowCollection<TRow>.Create<TRow, TKey> (source, keySelector, comparer, true);
		}

		public static EnumerableRowCollection<S> Select<TRow, S> (this TypedTableBase<TRow> source, Func<TRow, S> selector) where TRow : DataRow
		{
			return new EnumerableRowCollection<S> (Enumerable.Select<TRow, S> (source, selector));
		}

		public static EnumerableRowCollection<TRow> Where<TRow> (this TypedTableBase<TRow> source, Func<TRow, bool> predicate) where TRow : DataRow
		{
			return new EnumerableRowCollection<TRow> (Enumerable.Where<TRow> (source, predicate));
		}
	}
}
